<?php
/**
 * Created by PhpStorm.
 * User: Dick
 * Date: 2016/3/28
 * Time: 0:59
 */

namespace Model;

use Method\JSON;

class Message
{
    public $id;
    public $whos;
    public $content;
    public $from;
    public $hfnum;
    public $shoucang;
    public $fowardnum;
    public $img;
    public $datetime;


    /**
     * 发送weblog
     */
    public function Sendmood($whos, $content, $img, $datetime)
    {
        global $pdo;
        $inTransaction = $pdo->inTransaction();//判断事务开启
        if (!$inTransaction) {
            $pdo->beginTransaction();//开启事务
        }
        $statement = $pdo->prepare("INSERT INTO mood SET whos=?,content=?,`from`=?,hfnum=?,fowardnum=?,shoucang=?,image=?,datetime=?");
        $statement->bindValue(1, $whos);
        $statement->bindValue(2, $content);
        $statement->bindValue(3, $whos);
        $statement->bindValue(4, 0);
        $statement->bindValue(5, 0);
        $statement->bindValue(6, 0);
        $statement->bindValue(7, $img);
        $statement->bindValue(8, $datetime);
        $statement->execute();
        if (!$inTransaction) {
            $pdo->commit();
        }
    }


    /**
     * 获取weblog
     */
    public function Getmood($whos, $page = array('*'))
    {
        global $pdo;
        $start = $page['dataStart'];
        if ($start == null) {
            $start = 0;
        }
        $size = $page['pageSize'];
        $statament = $pdo->prepare('SELECT mood.*,user.username,user.nickname,user.avatar,image.photo,follow.following FROM mood LEFT JOIN user ON mood.whos = user.id LEFT JOIN follow ON user.id = follow.followed LEFT JOIN image ON mood.image=image.id WHERE follow.following =:whos ORDER BY mood.datetime DESC limit :datastart,:datanum');
        $statament->bindValue('whos', $whos);
        $statament->bindValue(':datastart', $start, $pdo::PARAM_INT);
        $statament->bindValue(':datanum', $size, $pdo::PARAM_INT);
        $statament->setFetchMode(\PDO::FETCH_ASSOC);
        $statament->execute();
        $data = $statament->fetchAll();
        $extra = array(
            'totalPage' => $page['totalPage'],
            'pageSize' => $page['pageSize']
        );
        JSON::json($data, 200, $extra);
    }

    //返回推送数目
    public static function count($userid, $moodid)
    {
        global $pdo;
        $statament = $pdo->prepare("SELECT COUNT(*) FROM mood LEFT JOIN follow ON mood.whos = followed WHERE mood.id>:moodid AND following=:id");
        $statament->bindValue(':moodid', $moodid);
        $statament->bindValue(':id', $userid);
        $statament->execute();
        echo $statament->fetchColumn();
    }

    /**
     * 个人主页的微博
     * */
    public function Getpersonalmood($whos, $page = array('*'))
    {
        global $pdo;
        $start = $page['dataStart'];
        if ($start == null) {
            $start = 0;
        }
        $size = $page['pageSize'];
        $statament = $pdo->prepare('SELECT mood.*,user.username,user.nickname,user.avatar,image.photo FROM mood LEFT JOIN user ON mood.whos = user.id LEFT JOIN image ON mood.image=image.id WHERE user.id =:whos ORDER BY mood.datetime DESC limit :datastart,:datanum');
        $statament->bindValue(':whos', $whos);
        $statament->bindValue(':datastart', $start, $pdo::PARAM_INT);
        $statament->bindValue(':datanum', $size, $pdo::PARAM_INT);
        $statament->setFetchMode(\PDO::FETCH_ASSOC);
        $statament->execute();
        $data = $statament->fetchAll();

        $extra = array(
            'totalPage' => $page['totalPage'],
            'pageSize' => $page['pageSize']
        );
        JSON::json($data, 200, $extra);
    }

    /**
     * 个人主页的pic微博
     * */
    public function GetPicmood($whos, $page = array('*'))
    {
        global $pdo;
        $start = $page['dataStart'];
        if ($start == null) {
            $start = 0;
        }
        $size = $page['pageSize'];
        $statament = $pdo->prepare('SELECT mood.*,user.username,user.nickname,user.avatar,image.photo FROM mood LEFT JOIN user ON mood.whos = user.id LEFT JOIN image ON mood.image=image.id WHERE user.id =:whos AND mood.image IS NOT NULL ORDER BY mood.datetime DESC limit :datastart,:datanum');
        $statament->bindValue(':whos', $whos);
        $statament->bindValue(':datastart', $start, $pdo::PARAM_INT);
        $statament->bindValue(':datanum', $size, $pdo::PARAM_INT);
        $statament->setFetchMode(\PDO::FETCH_ASSOC);
        $statament->execute();
        $data = $statament->fetchAll();

        $extra = array(
            'totalPage' => $page['totalPage'],
            'pageSize' => $page['pageSize']
        );
        JSON::json($data, 200, $extra);
    }

    /**
     * 删除微博
     * */
    public function Delmood()
    {
        global $pdo;
        $statament = $pdo->prepare("SELECT COUNT(*) FROM mood LEFT JOIN follow ON mood.whos = followed WHERE mood.id>:moodid AND following=:id");
        $statament->bindValue(':moodid', $moodid);
        $statament->bindValue(':id', $userid);
        $statament->execute();
        echo $statament->fetchColumn();
    }

}